using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MeiluluSqlite;
using MeiluluSqlite.DataMode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using netcoreTextMvc.Models;
using Newtonsoft.Json;

namespace netcoreTextMvc.Controllers
{
    public class MoneyController : Controller
    {

        public IActionResult Index(string id, string types, string number, string remarks, string date,string chartType)
        {
            if (String.IsNullOrEmpty(id))
            {
                return View();
            }
            if (!String.IsNullOrEmpty(types) && !String.IsNullOrEmpty(number) && !String.IsNullOrEmpty(remarks) && !String.IsNullOrEmpty(date))
            {
                AddRecord(id, types, number, remarks, date);
            }
            return TuBiao(id, chartType);
        }
        public IActionResult TuBiao(string id,string chartType)
        {
            if (String.IsNullOrEmpty(chartType))
            {
                ShowValue sv = new();
                sv.id = id;
                var list =Program.sqlite.Select<User>("username='" + id + "'");
                if (list.Count == 1)
                {
                    var mlist =Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' order by changeTime");
                    mlist.Sort((x, y) => x.changeTime.CompareTo(y.changeTime));//升序
                    double moneyAll = 0;
                    for (int i = 0; i < mlist.Count; i++)
                    {
                        if (mlist[i].type > 0)
                        {
                            moneyAll += mlist[i].value;
                        }
                        else
                        {
                            moneyAll -= mlist[i].value;
                        }
                        sv.date.Add(mlist[i].changeTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        sv.value.Add(moneyAll);
                    }
                    if (sv.date.Count == 0)
                    {
                        sv.date.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    if (sv.value.Count == 0)
                    {
                        sv.value.Add(0);
                    }

                }
                sv.ToStr();
                ViewData["data"] = sv;
               
            }
            else
            {
                var list =Program.sqlite.Select<User>("username='" + id + "'");
                if (list.Count == 1)
                {
                    var mlist =Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' order by changeTime");
                    ShowValue sv = new();
                    double moneyAll = 0;
                    double outvalue = 0;
                    double addvalue = 0;
                    for (int i = 0; i < mlist.Count; i++)
                    {
                        if (mlist[i].type > 0)
                        {
                            moneyAll += mlist[i].value;
                            addvalue += mlist[i].value;
                            sv.value.Add(mlist[i].value);
                        }
                        else
                        {
                            moneyAll -= mlist[i].value;
                            outvalue += mlist[i].value;
                            sv.value.Add(0 - mlist[i].value);
                        }
                        sv.date.Add(mlist[i].changeTime.ToString("yyyy-MM-dd HH:mm:ss"));

                    }
                    sv.outValue = outvalue;
                    sv.addValue = addvalue;
                    sv.nowValue = moneyAll;
                    if (sv.date.Count == 0)
                    {
                        sv.date.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    if (sv.value.Count == 0)
                    {
                        sv.value.Add(0);
                    }
                    sv.ToStr();
                    ViewData["data"] = sv;
                }
            }
            return View("TuBiao");
        }
        public IActionResult InfoList(string id,string classname)
        {
            List<MyMoney> myMoney = new();
            var list =Program.sqlite.Select<User>("username='" + id + "'");
            if (list.Count > 0)
            {
                if (string.IsNullOrEmpty(classname))
                {
                    myMoney =Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' order by changeTime");
                }
                else
                {
                    myMoney =Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' and remarks='" + classname + "' order by changeTime");
                }
            }
            myMoney.Sort((x, y) => x.changeTime.CompareTo(y.changeTime));//升序
            ViewData["data"] = myMoney;
            ViewData["id"] = id;
            return View("InfoList");
        }
        public string AddRecord(string id, string type, string number, string remarks, string date)
        {
            User user;
            var list =Program.sqlite.Select<User>("username='" + id + "'");
            if (list.Count > 1)
            {
                return "数据异常";
            }
            else if (list.Count == 0)
            {
                user = new User()
                {
                    creatTime = DateTime.Now,
                    password = "",
                    userid = Guid.NewGuid().ToString(),
                    username = id
                };
               Program.sqlite.Insert(user);
            }
            else
            {
                user = list[0];
            }
            if (!DateTime.TryParse(date, out DateTime crdate))
            {
                crdate = DateTime.Now;
            }
            MyMoney money = new()
            {
                changeTime = crdate,
                id = Guid.NewGuid().ToString(),
                userid = user.userid,
                remarks = remarks,
                type = Convert.ToInt32(type),
                value = Convert.ToDouble(number)
            };
            if (Program.sqlite.Insert(money))
            {
                return "添加完成";
            }
            else
            {
                return "添加失败";
            }

        }
        [HttpPost]
        public string Deldata(string id)
        {
            if (Program.sqlite.Delate<MyMoney>(new MyMoney() { id = id }))
            {
                return "删除完成";
            }
            else
            {
                return "删除失败";
            }
        }
    }
}
